import 'dart:math';

import 'package:enjoy_plus_community/routes/index.dart';
import 'package:flutter/material.dart';

class BuildingList extends StatefulWidget {
  BuildingList({super.key, required this.point});

  String point;

  @override
  State<BuildingList> createState() => _RoomListState();
}

class _RoomListState extends State<BuildingList> {
  @override
  void initState() {
    super.initState();
    fakeData(widget.point);
  }

  fakeData(point) {
    // 1.1 随机产生楼栋的数量
    Random random = Random();
    int size = random.nextInt(6) + 3; // 3-8
    // 1.2 确定小区楼栋的名称
    String type = size > 5 ? '单元' : '号楼';
    // 1.3 更新数据
    setState(() {
      data = {"point": point, "size": size, "type": type};
    });
  }

  Map data = {
    "point": "", // 记录小区信息
    "size": 0, // 随机产生楼栋的数量
    "type": "", // 小区的楼栋名称(size>4 单元, size<=4 号楼)
  };

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('选择楼栋')),
      body: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, AppRouter.roomList, arguments: {'point': data['point'], 'building': '${index+1}${data['type']}'});
            },
            child: Container(
              color: Colors.white,
              padding: const EdgeInsets.all(10),
              child: Row(
                children: [
                  Expanded(
                    child: Text('${data['point']}${index + 1}${data['type']}'),
                  ),
                  const Row(
                    children: [
                      Icon(
                        Icons.arrow_forward_ios,
                        size: 16,
                        color: Colors.black,
                      ),
                    ],
                  ),
                ],
              ),
            ),
          );
        },
        itemCount: data['size'],
      ),
    );
  }
}
